package com.Mobile.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.Mobile.Entity.Emotion;
import com.Service.EmotionService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class MobileEmotionAction implements ServletResponseAware{

	@Autowired
	private EmotionService emotionService;
	
	Emotion emotion=new Emotion();
	
	private String userid=null;
	private String emotionvalue=null;
	private String creatdate=null;
	private int flag=0;
	private HttpServletResponse response;
	private String creatdate1=null;
	
	public void emotion() throws ServletException, IOException{
		
		String userid=ServletActionContext.getRequest().getParameter("userid");
		String emotionvalue=ServletActionContext.getRequest().getParameter("emotionvalue");
		
	
		userid = java.net.URLDecoder.decode(userid,"UTF-8");
		emotionvalue = java.net.URLDecoder.decode(emotionvalue,"UTF-8");
		
		
		JSONObject returnjson=new JSONObject();
		JSONArray jsonarray=new JSONArray();
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8"); 
		try {
			PrintWriter out = response.getWriter();
			if(userid!=null&&userid!=null){
				//鐎电懓寮弫鎷岀箻鐞涘矁袙閻?
				flag=1;
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//鐠佸墽鐤嗛弮銉︽埂閺嶇厧绱?
				creatdate=df.format (new Date());
				
				System.out.println(creatdate);// new Date()娑撻缚骞忛崣鏍х秼閸撳秶閮寸紒鐔告闂?
				System.out.println(userid);
				System.out.println(emotionvalue);	
				
				int id=Integer.parseInt(userid);
				System.out.println(id);
				/*emotion=this.emotionService.getById(id);
				System.out.println(emotion);								
				if(null == emotion){
					Emotion emotion1=new Emotion();
					emotion1.setUserId(id);
					emotion1.setCreateDate(creatdate);
					emotion1.setEmotionValue(emotionvalue);
					this.emotionService.save(emotion1);
				}	  
					else{*/
						Emotion emotion=new Emotion();
						emotion.setUserId(id);
						emotion.setCreateDate(creatdate);
						emotion.setEmotionValue(emotionvalue);
						this.emotionService.save(emotion);													
				returnjson.put("flag",flag);
				returnjson.put("CreateDate",creatdate);
			}
			else
			{
				returnjson.put("flag", flag);
			}
			out.println(returnjson.toString());
			out.flush();
			out.close();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
		
	public void setServletResponse(HttpServletResponse response) {
		// TODO Auto-generated method stub
		this.response=response;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getEmotionvalue() {
		return emotionvalue;
	}

	public void setEmotionvalue(String emotionvalue) {
		this.emotionvalue = emotionvalue;
	}

	public String getCreatdate() {
		return creatdate;
	}

	public void setCreatdate(String creatdate) {
		this.creatdate = creatdate;
	}

	public EmotionService getEmotionService() {
		return emotionService;
	}

	public void setEmotionService(EmotionService emotionService) {
		this.emotionService = emotionService;
	}

	public Emotion getEmotion() {
		return emotion;
	}

	public void setEmotion(Emotion emotion) {
		this.emotion = emotion;
	}

}
